package org.ehe.business.engineer.service.impl;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.ehe.business.engineer.domain.EngineerLevelConfigEntity;
import org.ehe.business.engineer.mapper.EngineerLevelConfigMapper;
import org.ehe.business.engineer.service.EngineerLevelConfigService;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author :zhangnn
 * @className :EngineerLevelConfigServiceImpl
 * @description: TODO
 * @date 2025-07-26 16:51:16
 */
@Service
public class EngineerLevelConfigServiceImpl extends ServiceImpl<EngineerLevelConfigMapper, EngineerLevelConfigEntity> implements EngineerLevelConfigService {

    /**
     * 根据职级获取配置信息
     * @param level
     * @return
     */
    @Override
    public EngineerLevelConfigEntity getByLevel(String level) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<EngineerLevelConfigEntity>()
            .eq(EngineerLevelConfigEntity::getLevel, level));
    }

    /**
     * 获取职级配置列表
     * @return
     */
    @Override
    public List<EngineerLevelConfigEntity> queryList() {
        return this.baseMapper.selectList(new QueryWrapper<EngineerLevelConfigEntity>().eq("del_flag",0));
    }

    /**
     * 获取职级配置列表
     * @param configEntity
     * @return
     */
    @Override
    public List<EngineerLevelConfigEntity> queryConfigList(EngineerLevelConfigEntity configEntity) {
        List<EngineerLevelConfigEntity> configEntities = this.baseMapper.selectList(
            new LambdaQueryWrapper<EngineerLevelConfigEntity>()
                .eq(StringUtils.isNotBlank(configEntity.getLevel()),
                    EngineerLevelConfigEntity::getLevel, configEntity.getLevel())
                .like(StringUtils.isNotBlank(configEntity.getLevelName()),
                    EngineerLevelConfigEntity::getLevelName, configEntity.getLevelName())
                .orderByAsc(EngineerLevelConfigEntity::getLevel)
        );
        return configEntities;
    }

    /**
     * 保存职级配置
     * @param configEntity
     */
    @Override
    public String saveEngineerLevelConfig(EngineerLevelConfigEntity configEntity) {
        EngineerLevelConfigEntity existingEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<EngineerLevelConfigEntity>()
            .eq(EngineerLevelConfigEntity::getLevel, configEntity.getLevel()));
        if(Objects.nonNull(existingEntity)){
            return "["+configEntity.getLevel()+"]职级已存在，请勿重复配置";
        }

        configEntity.setCreateBy(LoginHelper.getUsername());
        configEntity.setTenantId(LoginHelper.getTenantId());
        int insert = this.baseMapper.insert(configEntity);
        return insert > 0 ? "新增职级配置成功" : "新增职级配置失败";
    }

    /**
     * 修改职级配置
     * @param configEntity
     * @return
     */
    @Override
    public String updateEngineerLevelConfig(EngineerLevelConfigEntity configEntity) {
        configEntity.setUpdateBy(LoginHelper.getUsername());
        EngineerLevelConfigEntity existConfig = this.baseMapper.selectById(configEntity.getId());

        if(!existConfig.getLevel().equals(configEntity.getLevel())){
            // 如果修改了职级，需要检查新职级是否已存在
            EngineerLevelConfigEntity duplicateEntity = this.baseMapper.selectOne(new LambdaQueryWrapper<EngineerLevelConfigEntity>()
                .eq(EngineerLevelConfigEntity::getLevel, configEntity.getLevel()));
            if(Objects.nonNull(duplicateEntity)){
                return "["+configEntity.getLevel()+"]职级已存在，请勿重复配置";
            }
        }

        int update = this.baseMapper.updateById(configEntity);
        return update > 0 ? "修改职级配置成功" : "修改职级配置失败";
    }

    /**
     * 删除职级配置
     * @param id
     * @return
     */
    @Override
    public int deleteConfig(Long id) {
        int i = this.baseMapper.deleteConfigById(id);
        return i;
    }
}
